package cn.hxlc.tie.demo.modular.ljt.equipment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hxlc.tie.demo.core.enums.StatusEnum;
import cn.hxlc.tie.demo.core.error.MdmExceptionEnum;
import cn.hxlc.tie.demo.modular.ljt.equipment.entity.Facility;
import cn.hxlc.tie.demo.modular.ljt.equipment.mapper.FacilityMapper_lt;
import cn.hxlc.tie.demo.modular.ljt.equipment.model.params.FacilityParam;
import cn.hxlc.tie.demo.modular.ljt.equipment.model.result.FacilityResult;
import cn.hxlc.tie.demo.modular.ljt.equipment.service.FacilityService_lt;
import cn.hxlc.tie.demo.modular.ljt.equipment.util.FNumberUtil;
import cn.hxlc.tie.libs.mp.page.PageFactory;
import cn.hxlc.tie.model.exp.ServiceException;
import cn.hxlc.tie.model.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service
@Transactional
public class FacilityServiceImpl_lt extends ServiceImpl<FacilityMapper_lt, Facility> implements FacilityService_lt {

    @Override
    public PageResult<FacilityResult> findPageBySpec(FacilityParam param) {
        Page pageContext = getPageContext();
        IPage<FacilityResult> page = this.baseMapper.customPageList(pageContext,param);
        return new PageResult<>(page);
    }

    @Override
    public List<Facility> fisMain() {
        QueryWrapper<Facility> wrapper = new QueryWrapper<>();
        wrapper.eq("fismain",1);
        List<Facility> list = this.baseMapper.selectList(wrapper);
        return list;
    }

    @Override
    public Facility selectById(Integer fid) {
        Facility facility = this.baseMapper.selectById(fid);
        return facility;
    }

    @Override
    public void addFacility(FacilityParam param) {
        try {
            this.baseMapper.insert(getEntity(param));
        } catch (Exception e) {
            throw new ServiceException(MdmExceptionEnum.ENABLED_ERROR.getCode(),"新增失败!");
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateFacility(FacilityParam param) {
        FNumberUtil.checkRepeatNumber(this.baseMapper,param.getFnumber(),param.getFid());
        Facility oldEntity = getOldEntity(param);
        Facility newEntity = getEntity(param);
        BeanUtil.copyProperties(newEntity,oldEntity);
        try {
            this.baseMapper.updateById(newEntity);
        } catch (Exception e) {
            throw new ServiceException(MdmExceptionEnum.ENABLED_ERROR.getCode(),"编辑失败!");
        }
    }

    private Facility getOldEntity(FacilityParam param) {
        return this.getById(getKey(param));
    }

    private Serializable getKey(FacilityParam param) {
        return param.getFid();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchChangeState(Object[] fid, StatusEnum toEnum) {
        Facility facility;
        for(int i=0; i<fid.length; i++){
            facility =this.getById(new Long(fid[i].toString()));
            if (facility.getFstatus()!=null && toEnum != null && !facility.getFstatus().equals(toEnum)){
                facility.setFstatus(toEnum);
                this.updateById(facility);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(Object[] fid) {
        Facility facility;
        for(int i=0; i<fid.length; i++){
            facility = this.getById(new Long(fid[i].toString()));
            if (facility.getFstatus() != null && facility.getFstatus().equals(StatusEnum.BASE)){
                throw new ServiceException(MdmExceptionEnum.ENABLED_ERROR.getCode(),"存在启用状态数据,删除失败!");
            }
            facility.setFstatus(StatusEnum.DELETED);
            this.updateById(facility);
        }
    }


    private Facility getEntity(FacilityParam param){
        Facility facility = new Facility();
        BeanUtil.copyProperties(param,facility);
        return facility;
    }


    private Page getPageContext() {
        return PageFactory.defaultPage();
    }

}
